﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Kakeibo.View {
    public partial class SummaryPanel : UserControl, IView {

        private class TreeNode : Aga.Controls.Tree.Node {
            public const int NUM_AMOUNT_COLUMNS = 10;
            private int[] amount_;

            public TreeNode(string categoryName) {
                CategoryName = categoryName;
                amount_ = new int[NUM_AMOUNT_COLUMNS];
                Balance = 0;
                Budget = 0;
                for(int i = 0; i < amount_.Length; i++){
                    amount_[i] = 0;
                }
            }

            public string CategoryName { get; set; }
            public int Budget { get; set; }
            public int Balance { get; set; }
            public int Amount0 { get{return amount_[0];} set{amount_[0] = value;} }
            public int Amount1 { get{return amount_[1];} set{amount_[1] = value;} }
            public int Amount2 { get{return amount_[2];} set{amount_[2] = value;} }
            public int Amount3 { get{return amount_[3];} set{amount_[3] = value;} }
            public int Amount4 { get{return amount_[4];} set{amount_[4] = value;} }
            public int Amount5 { get{return amount_[5];} set{amount_[5] = value;} }
            public int Amount6 { get{return amount_[6];} set{amount_[6] = value;} }
            public int Amount7 { get{return amount_[7];} set{amount_[7] = value;} }
            public int Amount8 { get{return amount_[8];} set{amount_[8] = value;} }
            public int Amount9 { get{return amount_[9];} set{amount_[9] = value;} }
            public int[] Amount { get { return amount_; } }
        }

        private Dictionary<int, TreeNode> nodeHash_;
        private Aga.Controls.Tree.TreeModel treeModel_;
        private Aga.Controls.Tree.TreeColumn[] amountColumns_;
        private Aga.Controls.Tree.NodeControls.NodeControl[] amountNodes_;

        public SummaryPanel() {
            InitializeComponent();

            nodeHash_ = new Dictionary<int, TreeNode>();
            treeModel_ = new Aga.Controls.Tree.TreeModel();
            summaryTreeView.Model = treeModel_;

            amountColumns_ = new Aga.Controls.Tree.TreeColumn[TreeNode.NUM_AMOUNT_COLUMNS];
            amountNodes_ = new Aga.Controls.Tree.NodeControls.NodeControl[TreeNode.NUM_AMOUNT_COLUMNS];

            for (int i = 0; i < TreeNode.NUM_AMOUNT_COLUMNS; i++){
                var col = new Aga.Controls.Tree.TreeColumn();
                col.Header = "column" + i;
                col.SortOrder = System.Windows.Forms.SortOrder.None;
                col.TooltipText = null;
                col.Width = 80;
                col.MaxColumnWidth = col.Width;
                col.MinColumnWidth = col.Width;
                col.TextAlign = HorizontalAlignment.Center;
                summaryTreeView.Columns.Insert(1 + i, col);

                var node = new Aga.Controls.Tree.NodeControls.NodeTextBox();
                node.DataPropertyName = "Amount" + i;
                node.IncrementalSearchEnabled = true;
                node.LeftMargin = 3;
                node.ParentColumn = col;
                node.TextAlign = HorizontalAlignment.Right;
                if (i == TreeNode.NUM_AMOUNT_COLUMNS - 1) { // last node
                    node.Font = new Font(node.Font, FontStyle.Bold);
                }
                summaryTreeView.NodeControls.Add(node);

                amountColumns_[i] = col;
                amountNodes_[i] = node;
            }
        }

        public System.Windows.Forms.Control FormControl
        {
            get { return this; }
        }

        public void SetAmountColumnHeadersAsMonthly(int focusedYear, int focusedMonth) {
            int year = focusedYear;
            int month = focusedMonth;
            for (int i = amountColumns_.Length - 1; i >= 0; i--) {
                amountColumns_[i].Header = year + "年" + month + "月";
                month--;
                if (month <= 0) {
                    month = 12;
                    year--;
                }
            }
        }

        public bool AddRow(string categoryName, int categoryId, int parentCategoryId) {
            var node = new TreeNode(categoryName);

            if (parentCategoryId == 0) {
                treeModel_.Nodes.Add(node);
            }
            else {
                var parentNode = nodeHash_[parentCategoryId];
                if (parentNode == null) {
                    return false;
                }
                parentNode.Nodes.Add(node);
            }
            nodeHash_.Add(categoryId, node);

            return true;
        }

        public void FitAmountColumnsToTreeViewSize() {
            int treeViewWidth = this.summaryTreeView.Width;
            int sumColumnsWidth = treeColumnCategory.Width + treeColumnBalance.Width + treeColumnBudget.Width;

            for (int i = amountColumns_.Length - 1; i >= 0; i--) {
                sumColumnsWidth += amountColumns_[i].Width;
                amountColumns_[i].IsVisible = sumColumnsWidth < treeViewWidth;
            }
        }
        
        // events
        public event System.EventHandler OnPanelSizeChanged {
            add {
                this.SizeChanged += value;
            }
            remove {
                this.SizeChanged -= value;
            }
        }

        private void summaryTreeView_Click(object sender, EventArgs e) {

        }
    }
}
